<template>
	<view class="page__content">
		<view class="">
			<button @click="startScan">开始扫描</button>
			<button @click="stopScan">停止扫描</button>
		</view>
		<u-list height="300rpx" style="padding: 10px;">
			<u-list-item v-for="(item, index) in deviceList" :key="index">
				<u-cell>
					<template #title>

						<view class="x-f">
							<Volume :num="item.RSSI"></Volume>
							<view style="color: #333;margin-left: 10px;">{{item.name}}</view>
						</view>
					</template>
					<view slot="value">
						<u-button text="连接" size="mini" type="success" @click="connectDevice(item)"></u-button>

					</view>
				</u-cell>
			</u-list-item>
		</u-list>
	</view>
</template>

<script>
	export default {
		props: {
			choiceDeviceInfo: {
				type: Object,
				default: {}
			}
		},
		data() {
			return {
				pageConfig: {},
				// 检索到的设备数据
				deviceList: [],
				deviceMap: {},
				agreement: null,
			}
		},
		created() {
			let that = this;
			// 首先通知底层根据协议创建蓝牙工具类
			console.log(this.choiceDeviceInfo);
			// 通知蓝牙初始化
			uni.$emit("bluetooth:handleCommand", {
				command: 'startInitial',
				deviceInfo: this.choiceDeviceInfo,
				callBack: (agreement) => {
					console.log(agreement);

					that.agreement = agreement;
					console.log(that.agreement);

					setTimeout(() => {
						that.startScan();
					}, 300)
				}
			});

			// 通知蓝牙开始扫描
			// uni.$emit("bluetooth:handleCommand",{
			// 	command: 'startScan',
			// });

		},
		mounted() {
			// 监听搜索到的蓝牙设备信息，组合成列表数据
		},
		onUnload() {
			this.agreement = {};
		},
		methods: {
			// 开始扫描
			startScan() {
				let that = this;
				// 开启扫描
				that.agreement.Link("", (devices) => {
					console.log(devices);

					//加入搜索到的设备列表,因为蓝牙搜索到的数据是一条条返回的,且可能是重复的
					let list = [...that.deviceList];
					for (let i = 0; i < devices.length; i++) {
						let dev = devices[i];
						//去重
						if (that.deviceMap[dev.deviceId] >= 0) {
							let index = that.deviceMap[dev.deviceId];
							//更新
							list.splice(index, 1, dev);
						} else {
							//新增
							list.push(dev);
							that.deviceMap[dev.deviceId] = list.length - 1;
						}
					}

					that.deviceList = list;
				})
			},
			// 停止扫描
			stopScan() {
				if (!this.$utils.isEmpty(this.agreement)) {
					this.agreement.StopFind();
				}
			},

			// 连接设备
			connectDevice(deviceObj) {
				let that = this;
				console.log(deviceObj);

				this.agreement.Link(deviceObj.deviceId, [], (devices) => {
					// 获取连接状态值
					console.log(devices);
					if (devices.status == 2500) {
						that.agreement.CallBackGetData((data) => {
							console.log(data);
							// uni.$emit("BluetoothData", data);
						});


						// 获取所有已连接数据
						let deviceList_his = that.$store.state.QmsModule.DeviceLinkConfig.deviceList_his;
						deviceList_his.push({
							deviceInfo: deviceObj,
							agreement: that.agreement,
							isLink: true
						})


						// 确认设备已连接，将数据存储到历史设备连接数据中去
						that.$store.commit('QmsModule/ChangeAndSetStateData', {
							Name1: 'DeviceLinkConfig.deviceList_his',
							data: deviceList_his
						});
					}

				});

			}
		}
	}
</script>

<style>

</style>